//
//  CellFrame.m
//  CustomTbCellOptimizationObjC
//
//  Created by liy on 2024/10/9.
//

#import "CellFrame.h"
#import "Status.h"

@interface CellFrame ()
@property (weak, nonatomic) Status *status;
@end

@implementation CellFrame

- (instancetype)initWithStatus:(Status *)status {
  if (self = [super init]) {
    _status = status;
  }
  
  return self;
}

- (CGFloat)cellHeight {
  if (_cellHeight == 0) {
      // head frame
    CGFloat margin = 10;
    self.headFrame = CGRectMake(margin, margin, 50, 50);
    
      // nickename frame
    CGFloat nickenameLabelX = CGRectGetMaxX(self.headFrame) + margin;
    CGFloat nickenameLabelY = CGRectGetMinY(self.headFrame);
    CGSize nickenameLabelSize = [self.status.name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}];
    self.nicknameFrame = CGRectMake(nickenameLabelX, nickenameLabelY, nickenameLabelSize.width, nickenameLabelSize.height);
    
      // vip frame
    if (self.status.isVip) {
      CGFloat vipImageViewX = CGRectGetMaxX(self.nicknameFrame) + margin;
      CGFloat vipImageViewY = CGRectGetMinY(self.nicknameFrame);
      self.vipFrame = CGRectMake(vipImageViewX, vipImageViewY, 14, 14);
    }
    
      // content frame
    CGFloat contentLabelX = CGRectGetMinX(self.headFrame);
    CGFloat contentLabelY = CGRectGetMaxY(self.headFrame) + margin;
    CGRect contentRect = [self.status.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * margin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.f]} context:nil];
    self.contentFrame = CGRectMake(contentLabelX, contentLabelY, contentRect.size.width, contentRect.size.height);
    
      // picture frame
    if (self.status.picture != nil) {
      CGFloat pictureLabelX = CGRectGetMinX(self.contentFrame);
      CGFloat pictureLabelY = CGRectGetMaxY(self.contentFrame) + margin;
      self.pictureFrame = CGRectMake(pictureLabelX, pictureLabelY, 70, 100);
      self.cellHeight = CGRectGetMaxY(self.pictureFrame) + margin;
    } else {
      self.cellHeight = CGRectGetMaxY(self.contentFrame) + margin;
    }
    
  }
  
  return _cellHeight;
}
@end
